package com.ljh.onlinetest.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.domain.UserEventLog;
import com.ljh.onlinetest.domain.other.KeyValue;
import com.ljh.onlinetest.repository.UserEventLogMapper;
import com.ljh.onlinetest.service.UserEventLogService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.viewmodel.admin.user.UserEventPageRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljh
 * @ClassName UserEventLogServiceImpl.java
 * @createTime 2022年01月06日
 */
@Service
public class UserEventLogServiceImpl extends BaseServiceImpl<UserEventLog> implements UserEventLogService {

    private final UserEventLogMapper userEventLogMapper;

    @Autowired
    public UserEventLogServiceImpl(UserEventLogMapper userEventLogMapper) {
        super(userEventLogMapper);
        this.userEventLogMapper = userEventLogMapper;
    }

    @Override
    public PageInfo<UserEventLog> page(UserEventPageRequestVM model) {
        return PageHelper.startPage(model.getPageIndex(),model.getPageSize(),"id desc").doSelectPageInfo(() -> {
            userEventLogMapper.page(model);
        });
    }

    @Override
    public List<Integer> selectMothCount() {
        Date monthStartDay = DateTimeUtil.getMonthStartDay();
        Date monthEndDay = DateTimeUtil.getMonthEndDay();
        List<KeyValue> keyValues = userEventLogMapper.selectCountByDate(monthStartDay, monthEndDay);
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream().map(md -> {
            KeyValue keyValue = keyValues.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null);
            return null == keyValue ? 0 : keyValue.getValue();
        }).collect(Collectors.toList());
    }

    @Override
    public List<UserEventLog> getUserEventLogByUserId(Integer id) {
        return userEventLogMapper.getUserEventLogByUserId(id);
    }
}
